package multiple.thread.copy;

import java.io.IOException;

public class WorkThread extends Thread {
	private long begin, end;// 文件块读取的起始和结束位置
	private String sourceFile;
	private String desFile;

	public WorkThread(long begin, long end, String sourceFile, String desFile) {
		this.begin = begin;
		this.end = end;
		this.sourceFile = sourceFile;
		this.desFile = desFile;
	}

	@Override
	public void run() {
		byte[] buffer = new byte[4096];// 设置缓冲区
		int len = 0;
		try {
			ReadFrom rf = new ReadFrom(sourceFile);
			WriteTo wt = new WriteTo(desFile);
			long startTime = System.currentTimeMillis();
			while (begin < end) {
				len = rf.read(buffer, begin);
				// 判断读取的数据是否超出了文件块的结束位置end
				if (begin + len > end)
					len = (int) (end - begin);
				wt.write(buffer, len, begin);

				begin += len;// 起始位置往前移动len个字节
			}
			rf.close();
			wt.close();
			long endTime = System.currentTimeMillis();
			System.out.println(endTime - startTime);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
